package xcu.lxj.nettychat.common.event.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;
import xcu.lxj.nettychat.chat.constant.MQConstant;
import xcu.lxj.nettychat.chat.domain.dto.MsgSendMessageDTO;
import xcu.lxj.nettychat.chat.utils.MQProducer;
import xcu.lxj.nettychat.common.event.MessageSendEvent;

/**
 * @Author XinJian Lei
 * @Date 2024/9/28 17:49
 * @Version 1.0
 */

@Service
public class MessageSendListener {

    @Autowired
    private MQProducer mqProducer;



    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = MessageSendEvent.class, fallbackExecution = true)
    public void messageRoute(MessageSendEvent event) {
        // 获取 消息id
        Long msgId = event.getMsgId();
        // 发送给消息队列
        mqProducer.sendSecureMsg(MQConstant.SEND_MSG_TOPIC, new MsgSendMessageDTO(msgId), msgId);
    }

}


